import React, {useState} from 'react';
import {Form, Input, message, Modal} from 'antd';
import {http} from 'libs';
import store from './store';

export default function IForm() {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);

  function handleSubmit() {
    setLoading(true);
    const formData = form.getFieldsValue();
    formData['id'] = store.record.id;
    formData['role_ids'] = typeof formData.role_ids === 'number' ? [formData.role_ids] : formData.role_ids;
    http.post('/api/bank/', formData)
      .then(() => {
        message.success('操作成功');
        store.set('formVisible', false);
        store.fetchRecords()
      })
      .finally(() => setLoading(false))

  }
  // id=1,sn=zs,name=招行卡,card_issuer=招商银行,card_number=410xxx
  return (
    <Modal
      open
      maskClosable={false}
      title={store.record.id ? '编辑银行账户' : '添加银行账户'}
      onCancel={store.vSet('formVisible', false)}
      confirmLoading={loading}
      onOk={handleSubmit}>
      <Form autoComplete='off' form={form} initialValues={store.record} labelCol={{span: 6}} wrapperCol={{span: 14}}>

        <Form.Item required name="name" label="银行账户名称">
          <Input placeholder="请输入"/>
        </Form.Item>
        <Form.Item required name="sn" label="银行账户编号">
          <Input placeholder="请输入"/>
        </Form.Item>
        <Form.Item required name="card_issuer" label="所属银行">
          <Input placeholder="请输入"/>
        </Form.Item>
        <Form.Item required name="card_number" label="银行账号">
          <Input placeholder="请输入"/>
        </Form.Item>
      </Form>
    </Modal>
  )
}